<template>
  <div class="register-container">
    <div style="width: 520px; margin: 0 auto">
      <el-form
        ref="userFrom"
        :model="user"
        :rules="userRules"
        class="register-form"
        auto-complete="on"
        label-position="left"
      >
        <!-- 标题 -->
        <div class="title-container">
          <h3 class="title">实验室管理系统注册</h3>
        </div>
        <el-form-item prop="userName">
          <el-input ref="userName" v-model.trim="user.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item prop="trueName">
          <el-input ref="trueName" v-model.trim="user.trueName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input ref="password" v-model.trim="user.password" :type="passwordType" placeholder="请输入用户密码" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="userSex">
          <el-select v-model="user.userSex" placeholder="请选择用户性别" style="width: 100%">
            <el-option v-for="item in sexOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item prop="userEmail">
          <el-input ref="userEmail" v-model.trim="user.userEmail" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item prop="roleId">
          <el-select ref="roleId" v-model="user.roleId" placeholder="请选择用户身份" style="width: 100%">
            <el-option v-for="item in role" :key="item.id" :label="item.roleName" :value="item.id" />
          </el-select>
        </el-form-item>
        <!-- 按钮 -->
        <el-button type="primary" style="width: 30%; margin-bottom: 30px; float: left" @click.native.prevent="black"
          >返回登陆
        </el-button>
        <el-button
          :loading="loading"
          type="primary"
          style="width: 30%; margin-bottom: 30px; float: right"
          @click.native.prevent="saveUser"
          >注册</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import userApi from '@/api/sys/user'

export default {
  data() {
    return {
      sexOptions: ['男', '女', '未知'],

      // 加载状态
      loading: false,

      // 定义表单绑定对象
      user: {
        userName: undefined,
        trueName: undefined,
        password: undefined,
        userSex: undefined,
        userEmail: undefined,
        roleid: ''
      },
      role: [
        { id: 1, roleName: '管理员' },
        { id: 2, roleName: '教师' },
        { id: 3, roleName: '学生' }
      ],
      userRules: {
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' },
          { min: 5, message: '账号不能少于5位', trigger: 'blur' }
        ],
        trueName: [{ required: true, message: '昵称不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码不能少于5位', trigger: 'blur' }
        ],
        userSex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        userEmail: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '邮箱格式错误', trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请选择用户身份', trigger: 'change' }]
      },
      passwordType: 'password'
    }
  },
  methods: {
    // 显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 添加
    saveUser() {
      this.$refs.userFrom.validate((valid) => {
        if (valid) {
          this.loading = true
          userApi.save(this.user).then((response) => {
            // 提示
            this.$message.success('注册成功!')
            // 跳转列表页面，使用路由跳转方式实现
            this.$router.push({
              path: '/login'
            })
          })
        } else {
          return false
        }
      })
    },
    // 返回登陆
    black() {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$cursor: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .el-input {
    display: inline-block;
    height: 47px;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
